package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessLottery;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyPrizeListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.MyPrizeListHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/3/23 10:28
 * 修改备注：
 * ================================================
 */
public class MyPrizeListActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private List<MyPrizeListVO> voList = new ArrayList<>();

    private View view;
    private LinearLayout layoutFooter;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_my_prize);
        setTitle("我的奖品");
    }

    @Override
    public void initMember() {

        initFooterView();

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MyPrizeListHolder(parent);
            }
        });

        adapter.addFooter(new RecyclerArrayAdapter.ItemView() {
            @Override
            public View onCreateView(ViewGroup parent) {
                return view;
            }

            @Override
            public void onBindView(View headerView) {

            }
        });

        onRefresh();
    }

    private void initFooterView() {
        view = LayoutInflater.from(this).inflate(R.layout.view_my_prize, null);
        layoutFooter = view.findViewById(R.id.layout);

        layoutFooter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GotoUtil.gotoActivity(MyPrizeListActivity.this, PrizeNoticeActivity.class);
            }
        });
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                final MyPrizeListVO vo = (MyPrizeListVO) adapter.getAllData().get(position);
                if (vo.getAwardState().equals("0"))
                    new MaterialDialog.Builder(MyPrizeListActivity.this)
                            .title("提示")
                            .content("非门店工作人员请点击“取消”，若点击“确定”，奖品将自动失效！")
                            .positiveText("确定")
                            .negativeText("取消")
                            .onPositive(new MaterialDialog.SingleButtonCallback() {
                                @Override
                                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                    BusinessLottery.lottmageUpdate(MyPrizeListActivity.this, vo.getId(), mHandler);
                                }
                            })
                            .show();
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_RAFFLE_INFO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_RAFFLE_INFO_FIELD:
                        if (adapter.getAllData().size() > 0)
                            adapter.pauseMore();
                        else
                            recyclerView.showEmpty();
                        break;
                    case MSG.MSG_LOTTMAGE_UPDATE_SUCCESS:
                        showToast("兑换成功");
                        onRefresh();
                        break;
                    case MSG.MSG_LOTTMAGE_UPDATE_FIELD:
                        showToast(msg.obj + "");
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        voList = FastJsonUtil.getListBean(object.toString(), "body", "list", MyPrizeListVO.class);


        if (voList == null)
            return;

        if (voList.size() > 0) {
            layoutFooter.setVisibility(View.VISIBLE);
        } else
            layoutFooter.setVisibility(View.GONE);


        if (page == 1)
            adapter.clear();

        adapter.addAll(voList);

        if (voList.size() < limit)
            adapter.stopMore();
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessLottery.raffleInfo(this, page, limit, mHandler);
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            BusinessLottery.raffleInfo(this, page, limit, mHandler);
        }
    }


}
